<div class="row" style="padding-left: 50px; padding-right: 150px;">
  <div class="row flex-items-xs-between flex-items-xs-top" style="padding-left: 15px; padding-right: 15px;">
    <h3 class="header-title" id="创建计划任务模版">创建计划任务模版</h3>
  </div>
  <div style="position: fixed;right: 0;top: 100px; width: 200px; max-height: calc(100% - 160px); overflow-y: auto;">
    <wayne-navigation container=".content-area" [node]="naviList"></wayne-navigation>
  </div>
  <form #ngForm="ngForm" style="padding-right: 30px;width: 100%">
    <section class="form-block wrap">
      <div>
        <label class="label-level1" id="发布信息">发布信息</label>
        <div class="form-group form-group-padding">
          <label class="col-md-3 form-group-label-override required">发布说明</label>
          <textarea name="description" [(ngModel)]="cronjobTpl.description" rows="3" style="width: 65%" required>
          </textarea>
        </div>
      </div>
    </section>
    <section class="form-block wrap">
      <div>
        <label class="label-level1" id="计划任务配置">计划任务配置</label>
        <div style="padding-top:10px">
          <label class="col-md-12 form-group-label-override required">
            <span class="tooltip" style="color:red;">
              k8s调度任务的时区是UTC(+0)，按小时定期执行的任务，请减去8小时调整为北京时区(+8)<br>
              例如想在北京时间每天11点(0 11 * * *)执行定时任务，则设置为(0 2 * * *)
            </span>
          </label>
        </div>
        <div style="margin-top: 15px" class="form-group form-group-padding">
          <label class="col-md-3 form-group-label-override required">运行周期
            <a role="tooltip" aria-haspopup="true" class="tooltip tooltip-lg">
              <clr-icon shape="info-circle" size="24"></clr-icon>
              <span class="tooltip-content">计划任务的定时周期,Cron表达式</span>
            </a>
          </label>
          <label aria-haspopup="true" role="tooltip" [class.invalid]="!isScheduleValid" class="tooltip tooltip-validation tooltip-md tooltip-bottom-left">
            <input type="text" name="schedule"
                   [(ngModel)]="kubeCronjob.spec.schedule"
                   size="40"
                   required
                   class="tooltip tooltip-validation tooltip-md tooltip-bottom-left"
              placeholder="cron表达式">
            <span class="tooltip-content">
              cron表达式填写不正确
            </span>
            <a target="_blank" href="https://en.wikipedia.org/wiki/Cron">More</a>
          </label>
        </div>
        <div class="form-group form-group-padding">
          <label class="col-md-3 form-group-label-override required">并行策略
            <a role="tooltip" aria-haspopup="true" class="tooltip tooltip-lg">
              <clr-icon shape="info-circle" size="24"></clr-icon>
              <span class="tooltip-content">允许:支持并行JOB;禁止:如果之前的JOB未完成，下一个JOB禁止启动;替代:会取消当前未完成JOB，替换成新JOB</span>
            </a>
          </label>
          <label aria-haspopup="true" role="tooltip" class="tooltip tooltip-validation tooltip-md tooltip-bottom-left">
            <div class="select">
              <select name="concurrencyPolicy" [(ngModel)]="kubeCronjob.spec.concurrencyPolicy">
                <option value="Allow" selected>允许</option>
                <option value="Forbid">禁止</option>
                <option value="Replace">替代</option>
              </select>
            </div>
          </label>
        </div>
        <div class="form-group form-group-padding">
          <label class="col-md-3 form-group-label-override required">最大执行时间
            <a role="tooltip" aria-haspopup="true" class="tooltip tooltip-lg">
              <clr-icon shape="info-circle" size="24"></clr-icon>
              <span class="tooltip-content">任务执行后，最大的执行时间，超出后会被中止</span>
            </a>
          </label>
          <label aria-haspopup="true" role="tooltip" [class.invalid]="formValid('activeDeadlineSeconds')" class="tooltip tooltip-validation tooltip-md tooltip-bottom-left">
            <input type="number" name="activeDeadlineSeconds" [(ngModel)]="kubeCronjob.spec.jobTemplate.spec.activeDeadlineSeconds" size="45" required
                 placeholder="最大执行时间"> 秒
          </label>
        </div>
        <!--
        <div class="form-group form-group-padding">
          <label class="col-md-3 form-group-label-override required">最大成功历史
            <a role="tooltip" aria-haspopup="true" class="tooltip tooltip-lg">
              <clr-icon shape="info-circle" size="24"></clr-icon>
              <span class="tooltip-content">最大保留成功记录的数量。</span>
            </a>
          </label>
          <label aria-haspopup="true" role="tooltip" [class.invalid]="formValid('successfulJobsHistoryLimit')" class="tooltip tooltip-validation tooltip-md tooltip-bottom-left">
            <input type="number" name="successfulJobsHistoryLimit" [(ngModel)]="kubeCronjob.spec.successfulJobsHistoryLimit" size="45" required
                   placeholder="最大执行时间">
          </label>
        </div>
        <div class="form-group form-group-padding">
          <label class="col-md-3 form-group-label-override required">最大失败历史
            <a role="tooltip" aria-haspopup="true" class="tooltip tooltip-lg">
              <clr-icon shape="info-circle" size="24"></clr-icon>
              <span class="tooltip-content">最大保留失败记录的数量</span>
            </a>
          </label>
          <label aria-haspopup="true" role="tooltip" [class.invalid]="formValid('failedJobsHistoryLimit')" class="tooltip tooltip-validation tooltip-md tooltip-bottom-left">
            <input type="number" name="failedJobsHistoryLimit" [(ngModel)]="kubeCronjob.spec.failedJobsHistoryLimit" size="45" required
                   placeholder="最大执行时间">
          </label>
        </div>-->
      </div>
    </section>
    <section *ngFor="let container of kubeCronjob.spec.jobTemplate.spec.template.spec.containers; let i = index" class="form-block wrap">
      <div>
        <label class="label-level1" [id]="i ? '容器配置' + i : '容器配置'">容器配置
          <clr-icon (click)="onDeleteContainer(i)" shape="trash" title="删除容器" class="is-solid"></clr-icon>
        </label>
        <div class="container-box">
          <label class="label-level2" [id]="i ? '镜像配置' + i : '镜像配置'">镜像配置</label>
          <div class="form-group form-group-padding">
            <label class="col-md-3 form-group-label-override required">容器名称</label>
            <label aria-haspopup="true" role="tooltip" [class.invalid]="containerIsInvalid(i,'container.name_')" class="tooltip tooltip-validation tooltip-md tooltip-bottom-left">
              <input type="text" size="90" class="col-sm-12" required name="container.name_{{i}}" pattern="[a-z]([-a-z0-9]*[a-z0-9])?" [(ngModel)]="container.name"
                placeholder="英文，dns域名规则">
              <span class="tooltip-content">
                容器名称不符合规范，验证规则[a-z]([-a-z0-9]*[a-z0-9])?
              </span>
            </label>
          </div>
          <div class="form-group form-group-padding">
            <label class="col-md-3 form-group-label-override required">镜像地址</label>
            <label aria-haspopup="true" role="tooltip" [class.invalid]="containerIsInvalid(i,'container.image_')" class="tooltip tooltip-validation tooltip-md tooltip-bottom-left">
              <input type="text" size="90" class="col-sm-12" name="container.image_{{i}}" required pattern="{{getImagePrefixReg()}}" [(ngModel)]="container.image"
                placeholder="镜像地址">
              <span class="tooltip-content">
                镜像不符合校验规则 {{getImagePrefixReg()}}
              </span>
            </label>
          </div>
          <div class="form-group form-group-padding">
            <label class="col-md-3 form-group-label-override required">内存容量</label>
            <label aria-haspopup="true" [class.invalid]="!checkMemory(container.resources.limits.memory)" style="padding-right: 0;margin-right: 0;padding-left: 0"
              role="tooltip" class="col-md-6 tooltip tooltip-validation tooltip-md tooltip-bottom-left">
              <input type="number" style="width:inherit" name="container.resources.limits.memory_{{i}}" required [(ngModel)]="container.resources.limits.memory"
                placeholder="单位G">
              <span class="tooltip-content">
                内存大小应该在0~{{memoryLimit}}G之间
              </span>
            </label>
            <label class="col-md-3">G&nbsp;&nbsp;&nbsp;
            </label>
          </div>
          <div class="form-group form-group-padding">
            <label class="col-md-3 form-group-label-override required">CPU核数</label>
            <label aria-haspopup="true" [class.invalid]="!checkCpu(container.resources.limits.cpu)" style="padding-right: 0;margin-right: 0;padding-left: 0"
              role="tooltip" class="col-md-6 tooltip tooltip-validation tooltip-md tooltip-bottom-left">
              <input type="number" name="container.resources.limits.cpu_{{i}}" [(ngModel)]="container.resources.limits.cpu" style="width:inherit"
                required placeholder="单位个">
              <span class="tooltip-content">
                CPU大小应该在0~{{cpuLimit}}核之间
              </span>
            </label>
            <label class="col-md-3">核&nbsp;&nbsp;&nbsp;
            </label>
          </div>

        </div>

        <div class="container-box">
          <label class="label-level2" [id]="i ? '环境变量配置' + i : '环境变量配置'">环境变量配置
            <button (click)="onAddEnvFrom(i)" style="margin-left: 10px;" class="wayne-button primary"><clr-icon shape="add-text"></clr-icon>批量加载</button>
            <button (click)="onAddEnv(i)" style="margin-left: 10px;" class="wayne-button primary"><clr-icon shape="add"></clr-icon>逐个加载</button>
          </label>
          <div *ngFor="let envFrom of container.envFrom; let j = index">
            <div class="form-group form-group-padding">
              <label for="envFromType-{{i}}-{{j}}" class="col-md-3 form-group-label-override required">批量加载
                <a role="tooltip" aria-haspopup="true" class="tooltip tooltip-md">
                  <clr-icon shape="info-circle" size="24"></clr-icon>
                  <span class="tooltip-content">批量从配置集或加密字典加载环境变量</span>
                </a>
              </label>
              <div class="select">
                <select id="envFromType-{{i}}-{{j}}" name="envFrom.type_{{i}}_{{j}}" (change)="envFromChange($event.target.value,i,j)">
                  <option value="1" [selected]="envFrom.configMapRef">从配置集加载</option>
                  <option value="2" [selected]="envFrom.secretRef">从加密字典加载</option>
                </select>
              </div>

              <clr-icon style="margin-top: 10px;margin-right: 8px" (click)="onAddEnvFrom(i)" shape="plus-circle" title="添加环境变量" class="is-solid"></clr-icon>
              <clr-icon style="margin-top: 10px" (click)="onDeleteEnvFrom(i,j)" shape="trash" title="删除环境变量" class="is-solid"></clr-icon>
            </div>
            <div *ngIf="envFrom" class="form-group form-group-padding">
              <input type="text" size="40" name="envFrom.prefix_{{i}}_{{j}}" [(ngModel)]="envFrom.prefix" placeholder="为所有环境变量添加相同的前缀，可为空">
            </div>
            <div *ngIf="envFrom && envFrom.configMapRef" class="form-group form-group-padding">
              <input type="text" size="40" required name="envFrom.configMapRef.name_{{i}}_{{j}}" [(ngModel)]="envFrom.configMapRef.name"
                placeholder="配置集名称">
            </div>
            <div *ngIf="envFrom && envFrom.secretRef" class="form-group form-group-padding">
              <input type="text" size="40" required name="envFrom.secretRef.name_{{i}}_{{j}}" [(ngModel)]="envFrom.secretRef.name" placeholder="加密字典名称">
            </div>

          </div>

          <div *ngFor="let env of container.env; let j = index">
            <div class="form-group form-group-padding">
              <label for="envType-{{i}}-{{j}}" class="col-md-3 form-group-label-override required">逐个加载
                <a role="tooltip" aria-haspopup="true" class="tooltip tooltip-md">
                  <clr-icon shape="info-circle" size="24"></clr-icon>
                  <span class="tooltip-content">其它类型的环境变量只支持高级模式编辑</span>
                </a>
              </label>
              <div class="select">
                <select id="envType-{{i}}-{{j}}" name="env.type_{{i}}_{{j}}" (change)="envChange($event.target.value,i,j)">
                  <option value="0" [selected]="env.value!=null">自定义环境变量</option>
                  <option value="1" [selected]="env.valueFrom?.configMapKeyRef">从配置集加载</option>
                  <option value="2" [selected]="env.valueFrom?.secretKeyRef">从加密字典加载</option>
                  <option value="3" selected>其它</option>
                </select>
              </div>
              <clr-icon style="margin-top: 10px;margin-right: 8px" (click)="onAddEnv(i)" shape="plus-circle" title="添加环境变量" class="is-solid"></clr-icon>
              <clr-icon style="margin-top: 10px" (click)="onDeleteEnv(i,j)" shape="trash" title="删除环境变量" class="is-solid"></clr-icon>
            </div>
            <div class="form-group form-group-padding">
              <input [(ngModel)]="env.name" required name="env.name_{{i}}_{{j}}" type="text" size="45" placeholder="环境变量名称">
            </div>
            <div *ngIf="env.value != null" class="form-group form-group-padding">
              <input [(ngModel)]="env.value" required name="env.value_{{i}}_{{j}}" type="text" size="45" placeholder="环境变量值">
            </div>
            <div *ngIf="env.valueFrom && env.valueFrom.configMapKeyRef" class="form-group form-group-padding">
              <input type="text" size="40" required name="env.valueFrom.configMapKeyRef.name_{{i}}_{{j}}" [(ngModel)]="env.valueFrom.configMapKeyRef.name"
                placeholder="配置集名称">
              <input type="text" size="40" required name="env.valueFrom.configMapKeyRef.key_{{i}}_{{j}}" [(ngModel)]="env.valueFrom.configMapKeyRef.key"
                placeholder="配置集key">
            </div>
            <div *ngIf="env.valueFrom && env.valueFrom.secretKeyRef" class="form-group form-group-padding">
              <input type="text" size="40" required name="env.valueFrom.secretKeyRef.name_{{i}}_{{j}}" [(ngModel)]="env.valueFrom.secretKeyRef.name"
                placeholder="加密字典名称">
              <input type="text" size="40" required name="env.valueFrom.secretKeyRef.key_{{i}}_{{j}}" [(ngModel)]="env.valueFrom.secretKeyRef.key"
                placeholder="加密字典key">
            </div>

          </div>

        </div>

      </div>
    </section>
  </form>
</div>
<div class="clr-wizard-footer-buttons tpl-bottom" [style.top.px]="top">
  <button type="button" class="btn btn-outline" (click)="onCancel()">取消</button>
  <button type="button" class="btn btn-primary" (click)="onAddContainer()">添加容器</button>
  <button type="button" class="btn btn-primary" (click)="openModal()">高级配置</button>
  <button type="button" class="btn btn-primary" [disabled]="!isValid" (click)="onSubmit()">提交</button>
</div>

<wayne-ace-editor (outputObj)="saveCronjob($event)"></wayne-ace-editor>
